﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Activities;
using System.Activities.Core.Presentation;
using System.Activities.Presentation;
using System.Activities.Presentation.Metadata;
using System.Activities.Presentation.Toolbox;
using System.Activities.Statements;
using System.Windows.Controls;
using Microhard.CustomActivities.Activities;
using Microhard.ApproveFlow.Buz.Utility;

namespace Microhard.ApproveFlowDesigner
{
    public partial class frmFlowChartDesign : Form
    {
        public static bool IsShow;
        private WorkflowDesigner wd;
        private Grid mainGrid = new Grid();
        public Guid FlowChartID { set; get; }
        public string FlowChartName { set; get; }


        #region 绑定设计器
        private void RegisterMetadata()
        {
            DesignerMetadata dm = new DesignerMetadata();
            dm.Register();
        }

        private void AddDesigner()
        {
            //Create an instance of WorkflowDesigner class.
            this.wd = new WorkflowDesigner();

            //Place the designer canvas in the middle column of the grid.
            Grid.SetColumn(this.wd.View, 1);

            //加载文件
            var fc = FlowHelper.LoadActivityFromFile(this.FlowChartID);
            this.wd.Load(fc);

            //Add the designer canvas to the grid.
            mainGrid.Children.Add(this.wd.View);
        }

        private ToolboxControl GetToolboxControl()
        {
            // Create the ToolBoxControl.
            ToolboxControl ctrl = new ToolboxControl();

            // Create a category.
            ToolboxCategory category = new ToolboxCategory("流程工具");

            // Create Toolbox items.
            ToolboxItemWrapper tool1 =
                new ToolboxItemWrapper("System.Activities.Statements.FlowDecision",
                typeof(FlowDecision).Assembly.FullName, null, "判断");

            ToolboxItemWrapper tool2 = new ToolboxItemWrapper("Microhard.CustomActivities.Activities.SingleApproverActivity",
                typeof(SingleApproverActivity).Assembly.FullName, null, "单人审批");

            // Add the Toolbox items to the category.
            category.Add(tool1);
            category.Add(tool2);

            // Add the category to the ToolBox control.
            ctrl.Categories.Add(category);
            return ctrl;
        }

        private void AddToolBox()
        {
            ToolboxControl tc = GetToolboxControl();
            Grid.SetColumn(tc, 0);
            mainGrid.Children.Add(tc);
        }

        private void AddPropertyInspector()
        {
            Grid.SetColumn(wd.PropertyInspectorView, 2);
            mainGrid.Children.Add(wd.PropertyInspectorView);
        }
        #endregion

        public frmFlowChartDesign(Guid flowChartID, string flowChartName)
        {
            InitializeComponent();

            this.Text = flowChartName + ":" + flowChartID;
            this.FlowChartID = flowChartID;
            this.FlowChartName = flowChartName;

            mainGrid.ColumnDefinitions.Add(new ColumnDefinition());
            var chartcol = new ColumnDefinition();
            chartcol.Width = new System.Windows.GridLength(4, System.Windows.GridUnitType.Star);
            mainGrid.ColumnDefinitions.Add(chartcol);
            mainGrid.ColumnDefinitions.Add(new ColumnDefinition());

            this.RegisterMetadata();

            this.AddDesigner();

            this.AddToolBox();

            this.AddPropertyInspector();

            this.eleHostWFDesgin.Child = mainGrid;
        }

        private void frmFlowChartDesign_FormClosing(object sender, FormClosingEventArgs e)
        {
            IsShow = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + string.Format(@"\{0}.xaml", FlowChartID.ToString());
            this.wd.Save(path);
            //保存到数据库中
            FlowHelper.SaveXAMLInDB(path, FlowChartID);
        }
    }
}
